package v3.chapter2.synNotExtends;

public class Run {

	public static void main(String[] args) {
		Sub sref=new Sub();
		ThreadA a=new ThreadA(sref);
		a.setName("A");
		a.start();
		
		ThreadA b=new ThreadA(sref);
		b.setName("B");
		b.start();

	}

}


class ThreadB extends Thread{
	private Sub sub;
	public ThreadB(Sub sub) {
		super();
		this.sub=sub;
	}
	
	@Override 
	public void run() {
		sub.serviceMethod();
	}
}

class ThreadA extends Thread{
	private Sub sub;
	public ThreadA(Sub sub) {
		super();
		this.sub=sub;
	}
	
	@Override 
	public void run() {
		sub.serviceMethod();
	}
}


class Sub extends Main{
	@Override
	public void serviceMethod() {
		try {
			System.out.println("int main 下一步 sleep begin threadName="+Thread.currentThread().getName()+" time="+System.currentTimeMillis());
			Thread.sleep(5000);
			System.out.println("int main 下一步 sleep end threadName="+Thread.currentThread().getName()+" time="+System.currentTimeMillis());
			super.serviceMethod();
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}


class Main{
	synchronized public void serviceMethod() {
		try {
			System.out.println("int main 下一步 sleep begin threadName="+Thread.currentThread().getName()+" time="+System.currentTimeMillis());
			Thread.sleep(5000);
			System.out.println("int main 下一步 sleep end threadName="+Thread.currentThread().getName()+" time="+System.currentTimeMillis());
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}